//=====================================结构体============================================
#[derive(Debug)]
struct User {
    username: String,
    email: String,
    sign_in_count: u64,
    active: bool,
}

#[test]
fn test_struct(){
    let james = User{
        username: String::from("james"),
        email: String::from("12345@qq.com"),
        sign_in_count: 1,
        active: true,
    };

    println!("{:?}", james);

    let james_build = build_user(james);
    println!("{:?}", james_build);
}

fn build_user(user: User) -> User {
    User {
        username: String::from("james_build"),
        ..user
    }
}

// 元组结构体 （没有任何字段的类单元结构体）
#[derive(Debug)]
struct Color(i32, i32, i32);
#[test]
fn test_tuple_structs(){
    let black = Color(0,0,0);

    println!("{:?}", black);

    // 与普通结构体一样，通过“black.0”访问元组结构体成员
    println!("{} {}", black.0, black.1);
}

// 结构体方法
#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle{
    // 结构体-方法
    fn area(&self) -> u32 {
        self.width * self.height
    }

    fn can_hold(&self, other: &Rectangle) -> bool {
        self.width > other.width && self.height > other.height
    }
}

// 支持定义多个impl块
impl Rectangle{
    // 结构体-关联函数
    fn square(size: u32) -> Rectangle {
        Rectangle{
            width: size,
            height: size,
        }
    }
}

#[test]
fn test_struct_method(){
    let rect1 = Rectangle{width: 30, height: 50};
    println!("The area of the rectangle is {} square pixels.", rect1.area());

    let rect2 = Rectangle{width: 40, height: 20};
    let rect3 = Rectangle{width: 20, height: 20};

    // 带参数的方法
    println!("canhold {}", rect1.can_hold(&rect2));
    println!("canhold {}", rect1.can_hold(&rect3));

    // 关联函数
    let square = Rectangle::square(10);
    println!("square: {:?}", square);
}